package edu.maplef.bookwild.logic.impl;

import java.util.ArrayList;
import java.util.List;

import com.google.gdata.data.douban.SubjectEntry;
import com.google.gdata.data.douban.SubjectFeed;

import edu.maplef.bookwild.dao.IBookDao;
import edu.maplef.bookwild.dao.IBuddyDao;
import edu.maplef.bookwild.logic.IBookLogic;
import edu.maplef.bookwild.schema.Book;

public class BookLogicImpl extends BaseLogicImpl implements IBookLogic {
	private IBookDao bookDao;
	private IBuddyDao buddyDao;

	public IBookDao getBookDao() {
		return bookDao;
	}

	public void setBookDao(IBookDao bookDao) {
		this.bookDao = bookDao;
	}

	public void setBuddyDao(IBuddyDao buddyDao) {
		this.buddyDao = buddyDao;
	}

	public IBuddyDao getBuddyDao() {
		return buddyDao;
	}

	@Override
	public List<Book> getBookOfBuddy(String uid) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<Book> searchDoubanBook(String q, String tag, int startIndex,
			int maxResult) {
		try {
			SubjectFeed sf = getDoubanService().findBook(q, tag, startIndex,
					maxResult);
			List<SubjectEntry> se = sf.getEntries();
			List<Book> result = null;
			if (se.size() > 0)
				result = new ArrayList<Book>();
			for (SubjectEntry e : se) {
				try {
					Book b = bookDao.entry2Book(e);
					result.add(b);
				} catch (Exception ee) {
					continue;
				}
			}
			return result;

		} catch (Exception e) {
			log.warning(e.getMessage());
			return null;
		}
	}
}
